/* Compute Type I/II error rates for experts, estimators.
*/

clear
set more off
set scheme  s1mono 

*Set working directory to rd graph folder:
cd "${main}"

cap log close
log using "${logs}/average_error_rates.log", replace

use "${dat}/expert_merged_rd_estimates_micro.dta", clear

gen d324 = abs_disc > 0.323 & abs_disc < 0.325
gen d194415 = abs_disc > 0
gen d0 = abs_disc == 0

replace abs_disc = round(abs_disc,0.0001)

cap drop disc
gen disc  = 0 if abs_disc<=0.01
replace disc = 4 if abs_disc>=0.89 & abs_disc<=0.91
replace disc = 5 if abs_disc>=1.49
replace disc = 3 if abs_disc>=0.53 & abs_disc<=0.55
replace disc = 2 if abs_disc>=0.32 & abs_disc<=0.33
replace disc = 1 if abs_disc>=0.19 & abs_disc<=0.2

label define disc_lab 0 "0" 1 "0.1944" 2 "0.324" 3 "0.54" 4 "0.9" 5 "1.5"
label values disc disc_lab
tabulate disc, gen(d)

*Create cluster ID:
split filename, p("_")
rename filename7 playergraph_seed
egen dgp_by_seed = group(playerdgp playergraph_seed)
egen player = group(pid)

gen pq_rej = D_cl > 0 | D_cu < 0
gen ik_rej = rd_c_cl_ik > 0 | rd_c_cu_ik < 0
gen cct_c_rej = rd_c_cl_mse > 0 | rd_c_cu_mse < 0
gen cct_r_rej = rd_r_cl_mse > 0 | rd_r_cu_mse < 0
gen ak_rot_rej = rdh_taylor_rot_reject

gen y_nrej = 1 - y
gen pq_nrej = 1 - pq_rej
gen ik_nrej = 1 - ik_rej
gen cct_c_nrej = 1 - cct_c_rej
gen cct_r_nrej = 1 - cct_r_rej
gen ak_rot_nrej = 1 - ak_rot_rej

* Type I error rates
sum y pq_rej ik_rej cct_c_rej cct_r_rej ak_rot_rej if abs_disc == 0
* Type II error rates
sum y_nrej pq_nrej ik_nrej cct_c_nrej cct_r_nrej ak_rot_nrej if d324 == 1
*When using all nonzero discontinuities, weight all discontinuity magnitudes equally
preserve
keep if d194415==1
collapse (mean) y_nrej pq_nrej ik_nrej cct_c_nrej cct_r_nrej ak_rot_nrej, by(abs_disc)
sum y_nrej pq_nrej ik_nrej cct_c_nrej cct_r_nrej ak_rot_nrej
restore

* Regressions for p-values of difference in type II error rates b/w experts, estimators
* Structure data: observations we want to use humans for, and estimator obs (stacked regressions)
preserve
gen human = 1
egen pid_num = group(pid)
drop pid
gen pid = pid_num + 10000
drop pid_num
tempfile d
save "`d'", replace
restore

drop pid
gen pid = _n + 20000
gen human = 0
append using "`d'"

gen human_d1 = human*d1
gen human_d2 = human*d2
gen human_d3 = human*d3
gen human_d4 = human*d4
gen human_d5 = human*d5
gen human_d6 = human*d6

* Type I errors
replace y = pq_rej if human == 0
cgmreg y d1 human_d1 if abs_disc == 0, cluster(pid dgp_by_seed) nocons
replace y = ik_rej if human == 0
cgmreg y d1 human_d1 if abs_disc == 0, cluster(pid dgp_by_seed) nocons
replace y = cct_r_rej if human == 0
cgmreg y d1 human_d1 if abs_disc == 0, cluster(pid dgp_by_seed) nocons
replace y = ak_rot_rej if human == 0
cgmreg y d1 human_d1 if abs_disc == 0, cluster(pid dgp_by_seed) nocons


* Type II errors (0.324sigma)
replace y_nrej = pq_nrej if human == 0
cgmreg y_nrej d3 human_d3 if d324 == 1, cluster(pid dgp_by_seed) nocons
replace y_nrej = ik_nrej if human == 0
cgmreg y_nrej d3 human_d3 if d324 == 1, cluster(pid dgp_by_seed) nocons
replace y_nrej = cct_r_nrej if human == 0
cgmreg y_nrej d3 human_d3 if d324 == 1, cluster(pid dgp_by_seed) nocons
replace y_nrej = ak_rot_nrej if human == 0
cgmreg y_nrej d3 human_d3 if d324 == 1, cluster(pid dgp_by_seed) nocons

* Type II errors (all non-zero discontinuities)
replace y_nrej = pq_nrej if human==0
cgmreg y_nrej d2 human_d2 d3 human_d3 d4 human_d4 d5 human_d5 d6 human_d6 if disc!=0, cluster(pid dgp_by_seed) nocons
lincom 0.2*human_d2+0.2*human_d3+0.2*human_d4+0.2*human_d5+0.2*human_d6
replace y_nrej = ik_nrej if human==0
cgmreg y_nrej d2 human_d2 d3 human_d3 d4 human_d4 d5 human_d5 d6 human_d6 if disc!=0, cluster(pid dgp_by_seed) nocons
lincom 0.2*human_d2+0.2*human_d3+0.2*human_d4+0.2*human_d5+0.2*human_d6
replace y_nrej = cct_r_nrej if human==0
cgmreg y_nrej d2 human_d2 d3 human_d3 d4 human_d4 d5 human_d5 d6 human_d6 if disc!=0, cluster(pid dgp_by_seed) nocons
lincom 0.2*human_d2+0.2*human_d3+0.2*human_d4+0.2*human_d5+0.2*human_d6
replace y_nrej = ak_rot_nrej if human==0
cgmreg y_nrej d2 human_d2 d3 human_d3 d4 human_d4 d5 human_d5 d6 human_d6 if disc!=0, cluster(pid dgp_by_seed) nocons
lincom 0.2*human_d2+0.2*human_d3+0.2*human_d4+0.2*human_d5+0.2*human_d6



cap log close